home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / PIL / FontFile.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  3KB  |  121 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import os
  5. import Image
  6. import marshal
  7.  
  8. try:
  9.     import zlib
  10. except ImportError:
  11.     zlib = None
  12.  
  13. WIDTH = 800
  14.  
  15. def puti16(fp, values):
  16.     for v in values:
  17.         if v < 0:
  18.             v = v + 65536
  19.         
  20.         fp.write(chr(v >> 8 & 255) + chr(v & 255))
  21.     
  22.  
  23.  
  24. class FontFile:
  25.     bitmap = None
  26.     
  27.     def __init__(self):
  28.         self.info = { }
  29.         self.glyph = [
  30.             None] * 256
  31.  
  32.     
  33.     def __getitem__(self, ix):
  34.         return self.glyph[ix]
  35.  
  36.     
  37.     def compile(self):
  38.         if self.bitmap:
  39.             return None
  40.         
  41.         h = w = maxwidth = 0
  42.         lines = 1
  43.         for glyph in self:
  44.             if glyph:
  45.                 (d, dst, src, im) = glyph
  46.                 h = max(h, src[3] - src[1])
  47.                 w = w + (src[2] - src[0])
  48.                 if w > WIDTH:
  49.                     lines = lines + 1
  50.                     w = src[2] - src[0]
  51.                 
  52.                 maxwidth = max(maxwidth, w)
  53.                 continue
  54.         
  55.         xsize = maxwidth
  56.         ysize = lines * h
  57.         if xsize == 0 and ysize == 0:
  58.             return ''
  59.         
  60.         self.ysize = h
  61.         self.bitmap = Image.new('1', (xsize, ysize))
  62.         self.metrics = [
  63.             None] * 256
  64.         x = y = 0
  65.         for i in range(256):
  66.             glyph = self[i]
  67.             if glyph:
  68.                 (d, dst, src, im) = glyph
  69.                 xx = src[2] - src[0]
  70.                 yy = src[3] - src[1]
  71.                 x0 = x
  72.                 y0 = y
  73.                 x = x + xx
  74.                 if x > WIDTH:
  75.                     x = 0
  76.                     y = y + h
  77.                     x0 = x
  78.                     y0 = y
  79.                     x = xx
  80.                 
  81.                 s = (src[0] + x0, src[1] + y0, src[2] + x0, src[3] + y0)
  82.                 self.bitmap.paste(im.crop(src), s)
  83.                 self.metrics[i] = (d, dst, s)
  84.                 continue
  85.         
  86.  
  87.     
  88.     def save1(self, filename):
  89.         self.compile()
  90.         self.bitmap.save(os.path.splitext(filename)[0] + '.pbm', 'PNG')
  91.         fp = open(os.path.splitext(filename)[0] + '.pil', 'wb')
  92.         fp.write('PILfont\n')
  93.         fp.write(';;;;;;%d;\n' % self.ysize)
  94.         fp.write('DATA\n')
  95.         for id in range(256):
  96.             m = self.metrics[id]
  97.             if not m:
  98.                 puti16(fp, [
  99.                     0] * 10)
  100.                 continue
  101.             puti16(fp, m[0] + m[1] + m[2])
  102.         
  103.         fp.close()
  104.  
  105.     
  106.     def save2(self, filename):
  107.         self.compile()
  108.         data = marshal.dumps((self.metrics, self.info))
  109.         if zlib:
  110.             data = 'z' + zlib.compress(data, 9)
  111.         else:
  112.             data = 'u' + data
  113.         fp = open(os.path.splitext(filename)[0] + '.pil', 'wb')
  114.         fp.write('PILfont2\n' + self.name + '\n' + 'DATA\n')
  115.         fp.write(data)
  116.         self.bitmap.save(fp, 'PNG')
  117.         fp.close()
  118.  
  119.     save = save1
  120.  
  121.